package com.logic.landseaserver.service.callback;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.landseaserver.common.LandeaConstants.DdingConstant;
import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.domain.AbstractCallbackEvt;
import com.logic.landseaserver.domain.DdingEventCallbackEvt;
import com.logic.landseaserver.domain.DdingRoomSync;
import com.logic.landseaserver.persistence.read.DdingRoomSyncReadMapper;
import com.logic.landseaserver.persistence.write.DdingRoomSyncWriteMapper;

/**
 * 
 * 项目名称：system-server</br>
 * 类名称：DdingLockClearLockOffineAlarmEvtHandler</br>
 * 类描述：门锁上线回调</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年10月23日 下午2:39:43</br>
 * 
 * @version 1.0
 *
 */
@Service(DdingConstant.CALLBACK_EVNET_SERVICE_NAME_PREFIX + "clearLockOfflineAlarm")
public class DdingLockClearLockOffineAlarmEvtHandler implements IEventCallbackHandler
{
    private static final Logger LOG = LoggerFactory.getLogger(DdingLockClearLockOffineAlarmEvtHandler.class);
    
    @Autowired
    private DdingRoomSyncReadMapper ddingRoomSyncReadMapper;
    
    @Autowired
    private DdingRoomSyncWriteMapper ddingRoomSyncWriteMapper;
    
    @Override
    public Object process(AbstractCallbackEvt callbackEvent)
        throws LandseaException
    {
        try
        {
            DdingEventCallbackEvt event = (DdingEventCallbackEvt)callbackEvent;
            String roomIdStr = event.getRoom_id();
            if (StringUtils.isEmpty(roomIdStr))
            {
                LOG.error("云丁事件回调|门锁上线回调|room_id为空，消息丢弃处理.");
                throw LandseaException.createException(LandseaErrorMessageEnum.DDINGLOCK_ROOM_NONESYNC_ERROR);
            }
            
            Integer roomId = Integer.parseInt(roomIdStr);
            
            // 查询房间记录是否存在
            DdingRoomSync ddingRoomSync = ddingRoomSyncReadMapper.selectByPrimaryKey(roomId);
            
            if (null == ddingRoomSync || DdingConstant.SYNCSTATE_YES != ddingRoomSync.getSyncState())
            {
                LOG.error("云丁事件回调|门锁上线回调|房间记录不存在roomId=" + roomId);
                throw LandseaException.createException(LandseaErrorMessageEnum.DDINGLOCK_ROOM_NONESYNC_ERROR);
            }
            
            // 更新上线状态
            DdingRoomSync update = new DdingRoomSync();
            update.setOfflineFlag(DdingConstant.ONLINE_UPPERBATTERY);
            update.setRoomId(roomId);
            ddingRoomSyncWriteMapper.updateByPrimaryKeySelective(update);
            
            LOG.info("房间roomId=" + roomId + "门锁已上线.");
            
            return null;
        }
        catch (LandseaException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            LOG.error("云丁事件回调|门锁上线回调|处理消息发生错误.", e);
            throw LandseaException.createException(LandseaErrorMessageEnum.DDINGLOCK_CALLBACK_PROCESS_ERROR, e);
        }
    }
}
